package com.wyix.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @author wangdx
 * @since 2025/8/22
 */
public class DBUtil {
    public static final String URL = "jdbc:mysql://wyix.vm:3306/ecommerce?useSSL=false&serverTimezone=UTC";
    public static final String USER = "root";
    public static final String PASSWORD = "root";

    /**
     * 获取数据库连接
     * @return 数据库连接对象
     * @throws SQLException 数据库连接异常
     */
    public static Connection getConnection() throws SQLException {
        // JDBC 4.0+ 会自动加载驱动，无需显式调用Class.forName()
        return DriverManager.getConnection(URL, USER, PASSWORD);
    }

    /**
     * 关闭数据库连接
     * @param conn 要关闭的连接对象
     */
    public static void closeConnection(Connection conn) {
        if (conn!= null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
